<?php
/*
 Plugin Name: Wordpress FavIcon plugin
 Version: 0.02
 Plugin URI: http://code.google.com/p/koziolekwpplugins/
 Author: Koziolek
 Author URI: http://www.koziolekweb.pl
 Description: This plugin add favicon to your web side. Put file icon.png in main folder.

 This Plugin is in GNU GPLicense v3

 */

function wpIconPluginProccedPost(){
	if($_POST){
		if($_POST['wpFaviconFileName'])	{
			if(get_option("wpFaviconFileName")){
				update_option("wpFaviconFileName", $_POST['wpFaviconFileName']);
			}
			else{
				add_option("wpFaviconFileName", $_POST['wpFaviconFileName'], 'yes');
			}
		}
	}
}
wpIconPluginProccedPost();

function wpIconPluginRssHead(){
	$wpFaviconFileName = get_option("wpFaviconFileName");
	$siteURL= get_option("siteurl");
	if(!$wpFaviconFileName){
		$wpFaviconFileName ="icon.png";
	}
	$img ='<image>\n';
	$img =$img.'<url>'.${siteURL}.'/'.${wpFaviconFileName}.'</url>\n';
	$img =$img.'<title>'.get_option("blogname").'</title>\n';
	$img =$img.'<link>'.${siteURL}.'</link>\n';
	$img =$img.'<width></width>\n';
	$img =$img.'<height></height>\n';
	$img =$img.'</image>\n';
	echo $img;
}
function wpIconPluginAtomHead(){
	$wpFaviconFileName = get_option("wpFaviconFileName");
	$siteURL= get_option("siteurl");
	if(!$wpFaviconFileName){
		$wpFaviconFileName ="icon.png";
	}
	$img = '<icon>'.${siteURL}.'/'.${wpFaviconFileName}.'</icon>';
	echo $img."\n";
}

function wpIconPluginAdminMenu(){
	add_options_page('Wordpress FavIcon Options',
		'Wordpress FavIcon', 8, __FILE__, 'wpIconPluginPluginPptions');
}

function wpIconPluginPluginPptions(){
	$html = '<div class="wrap">';
	$html = $html.'<div id="icon-options-general" class="icon32"><br/></div>';
	$html = $html.'<h2>Wordpress Favicon Plugin</h2>';
	$html = $html.wpIconPluginMakeForm();
	$html = $html.wpIconPluginPreview();
	$html = $html.'</div>';

	echo $html;
}

function wpIconPluginPreview(){
	$wpFaviconFileName = get_option("wpFaviconFileName");
	$siteURL= get_option("siteurl");
	if(!$wpFaviconFileName){
		$wpFaviconFileName ="icon.png";
	}
	return "<img src=\"${siteURL}/${wpFaviconFileName}\" />";
}

function wpIconPluginMakeForm(){
	$wpFaviconFileName = get_option("wpFaviconFileName");
	if(!$wpFaviconFileName){
		$wpFaviconFileName ="icon.png";
	}
	$form = '<form method="post">';
	$form = $form .'<p>Enter name of file with icon. If empty then will use default file name icon.png</p>';
	$form = $form .'<input class="regular-text code" type="test" name="wpFaviconFileName" value="'.$wpFaviconFileName.'"/>';
	$form = $form .'<p class="submit"><input class="button-primary" type="submit" value="Save..."/></p>';
	$form = $form .'<form method="post">';
	return $form;
}

function wpIconPluginIcon(){
	$siteURL= get_option("siteurl");
	$wpFaviconFileName = get_option("wpFaviconFileName");
	if($wpFaviconFileName != false){
		print("<link rel=\"shortcut icon\" href=\"${siteURL}/${wpFaviconFileName}\" />");
	}
	else{
		print("<link rel=\"shortcut icon\" href=\"${siteURL}/icon.png\" />");
	}
}
add_filter( 'rss2_head', 'wpIconPluginRssHead' );
add_filter( 'rss_head', 'wpIconPluginRssHead' );
//add_filter( 'atom_head', 'wpIconPluginAtomHead' );
add_action('admin_menu', 'wpIconPluginAdminMenu');
add_action( 'wp_head', 'wpIconPluginIcon' );

?>